//
//  ViewController.m
//  UI06__相册作业
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 dllo. All rights reserved.
//
#import "ViewController.h"

#define WIDTH self.view.frame.size.width
#define HEIGHT self.view.frame.size.height

@interface ViewController ()
@property(nonatomic,retain)UIPageControl *myPage;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    
    
    
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    scrollView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:scrollView];
    [scrollView release];
    
    scrollView.contentSize = CGSizeMake(WIDTH * 8, 0);
    
    UIScrollView *scroll1 = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    imageView1.image = [UIImage imageNamed:@"7.jpg"];
    [scroll1 addSubview:imageView1];
    [imageView1 release];
    scroll1.minimumZoomScale = 0.5;
    scroll1.maximumZoomScale = 2;
    scroll1.delegate = self;
    [scrollView addSubview:scroll1];
    [scroll1 release];
    
    for (int i = 0; i < 7; i++) {
        UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT)];
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%d.jpg",i]];
        [scroll addSubview:imageView];
        [imageView release];
        scroll.minimumZoomScale = 0.5;
        scroll.maximumZoomScale = 2;
        scroll.delegate = self;
        [scrollView addSubview:scroll];
        [scroll release];
    }
    UIScrollView *scroll2 = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * 8, 0, WIDTH, HEIGHT)];
    UIImageView *imageView2 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    imageView2.image = [UIImage imageNamed:@"0.jpg"];
    [scroll2 addSubview:imageView2];
    [imageView2 release];
    scroll2.minimumZoomScale = 0.5;
    scroll2.maximumZoomScale = 2;
    scroll2.delegate = self;
    [scrollView addSubview:scroll2];
    [scroll2 release];
    
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.bounces = NO;
    scrollView.delegate = self;
    
    self.myPage = [[UIPageControl alloc] initWithFrame:CGRectMake(100, 650, 200, 50)];
    self.myPage.backgroundColor = [UIColor blackColor];
    [self.view addSubview:self.myPage];
    [_myPage release];
    self.myPage.numberOfPages = 7;
    self.myPage.alpha = 0.5;
    self.myPage.layer.masksToBounds = YES;
    // 点的外框加个弧度
    self.myPage.layer.cornerRadius = 10;
    [self.myPage addTarget:self action:@selector(pageAction:) forControlEvents:UIControlEventValueChanged];
    scrollView.tag = 1000001;
    scrollView.contentOffset = CGPointMake(WIDTH, 0);
    
    
    
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


-(void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(CGFloat)scale{
    UIScrollView *scroll = [scrollView.subviews firstObject];
    scroll.center = CGPointMake(WIDTH / 2, HEIGHT / 2);
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    UIScrollView *scroll = [scrollView.subviews firstObject];
    scroll.center = CGPointMake(WIDTH / 2, HEIGHT / 2);
}

-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    return [scrollView.subviews firstObject];
}


-(void)pageAction:(UIPageControl *)page{
    UIScrollView *scroll = [self.view viewWithTag:1000001];
    
   
    [scroll setContentOffset:CGPointMake(page.currentPage * WIDTH, 0) animated:YES];
}


-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    self.myPage.currentPage = scrollView.contentOffset.x / WIDTH;
    for (UIScrollView *small in scrollView.subviews) {
        if ([small isKindOfClass:[UIScrollView class]]) {
            small.zoomScale = 1.0;
        }
    }
    if (scrollView.contentOffset.x == WIDTH * 7) {
        scrollView.contentOffset = CGPointMake(WIDTH, 0);
    }
    if (scrollView.contentOffset.x == 0) {
        scrollView.contentOffset = CGPointMake(WIDTH * 7, 0);
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
